﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vnFingerPrint.DataAccess;
using vnFingerPrint.BusinessObject;
using vnCommonGUI;
namespace vnFingerPrint.GUI
{
    public partial class UsersManagement : Form
    {
        ImageList li = new ImageList();
        public UsersManagement()
        {
            InitializeComponent();
        }

        public void Bind_ListUsers()
        {
            li.Images.Clear();
            li.ImageSize = new Size(100, 100);
            li.TransparentColor = Color.Azure;
            listUsers.Items.Clear();
            listUsers.LargeImageList = li;
            foreach (Users_Object u in Users_DataAccess.SelectAll())
            {
                if (u.FullName.IndexOf(txtSearch.Text) < 0 && u.UserName.IndexOf(txtSearch.Text) < 0)continue;
                string imgurl = Application.StartupPath+"\\"+ Config_DataAccess.GetUserImagePath(u.ImageUrl);
                try{
                if (u.ImageUrl != "" && u.ImageUrl != null) li.Images.Add(u.UserName, new Bitmap(imgurl));
                else li.Images.Add(u.UserName, new Bitmap(512, 512));
                }catch 
                {
                }
                
                ListViewItem i = new ListViewItem(u.FullName,u.UserName);
                i.ToolTipText = u.UserName;
                listUsers.Items.Add(i);
            }
            listUsers.Refresh();
            
        }
        private void UsersManagement_Load(object sender, EventArgs e)
        {
            Bind_ListUsers();
           
        }

        private void listUsers_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listUsers_SelectedIndexChanged(object sender, MouseEventArgs e)
        {
            try
            {
                frmNewUser frm = new frmNewUser(listUsers.SelectedItems[0].ToolTipText);
                frm.ShowDialog();
            }
            catch
            {
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Bind_ListUsers();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            Bind_ListUsers();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                frmNewUser frm = new frmNewUser();
                frm.ShowDialog();
                Bind_ListUsers();
            }
            catch
            {
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listUsers.SelectedItems.Count < 1)
            {
                MessageBox.Show("Quí vị chưa chọn tài khoản cần sửa thông tin!");
                return;
            }
            try
            {
                frmNewUser frm = new frmNewUser(listUsers.SelectedItems[0].ToolTipText);
                frm.ShowDialog();
                Bind_ListUsers();
            }
            catch
            {
                
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listUsers.SelectedItems.Count < 1)
            {
                MessageBox.Show("Quí vị chưa chọn tài khoản cần xóa!");
                return;
            }
            if (MessageBox.Show("Quí vị có chắc là mình muốn xóa tài khoản này?", "Cảnh báo", MessageBoxButtons.YesNo) == DialogResult.No) return ;
            Users_Object delU = Users_DataAccess.GetObject(listUsers.SelectedItems[0].ToolTipText);
            Users_DataAccess.Delete(delU);
            Bind_ListUsers();

        }

        private void listUsers_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }
    }
}
